package com.scpii.api.domain.auth;

import java.io.Serializable;
import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.util.StringUtils;

import com.scpii.api.common.base.FilterEntity;

public class UsersInfoDetails extends FilterEntity implements UserDetails,
		Serializable {
	private Long id;

	private String userName;

	private String userAilas;

	private String password;

	private Date createDt;

	private Long createBy;

	private Date updateDt;

	private Long updateBy;

	private String authoritiesStr;

	private String userType;

	private String passwordFormat;

	private Long appId;

	private String avatar;

	private String provider;

	private String validCode;

	private static final long serialVersionUID = 1L;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName == null ? null : userName.trim();
	}

	public String getUserAilas() {
		return userAilas;
	}

	public void setUserAilas(String userAilas) {
		this.userAilas = userAilas == null ? null : userAilas.trim();
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password == null ? null : password.trim();
	}

	public Date getCreateDt() {
		return createDt;
	}

	public void setCreateDt(Date createDt) {
		this.createDt = createDt;
	}

	public Long getCreateBy() {
		return createBy;
	}

	public void setCreateBy(Long createBy) {
		this.createBy = createBy;
	}

	public Date getUpdateDt() {
		return updateDt;
	}

	public void setUpdateDt(Date updateDt) {
		this.updateDt = updateDt;
	}

	public Long getUpdateBy() {
		return updateBy;
	}

	public void setUpdateBy(Long updateBy) {
		this.updateBy = updateBy;
	}

	public String getUserType() {
		return userType;
	}

	public void setUserType(String userType) {
		this.userType = userType == null ? null : userType.trim();
	}

	public String getPasswordFormat() {
		return passwordFormat;
	}

	public void setPasswordFormat(String passwordFormat) {
		this.passwordFormat = passwordFormat == null ? null : passwordFormat
				.trim();
	}

	public Long getAppId() {
		return appId;
	}

	public void setAppId(Long appId) {
		this.appId = appId;
	}

	public String getAuthoritiesStr() {
		return authoritiesStr;
	}

	public void setAuthoritiesStr(String authoritiesStr) {
		this.authoritiesStr = authoritiesStr;
	}

	@Override
	public Collection<? extends GrantedAuthority> getAuthorities() {
		if (authoritiesStr == null) {
			return null;
		}
		String[] authoritiesInfos = StringUtils.split(authoritiesStr, ",");
		Set<GrantedAuthority> grantedAuthorities = new HashSet<GrantedAuthority>();
		for (String authoritiesInfo : authoritiesInfos) {
			grantedAuthorities.add(new SimpleGrantedAuthority(authoritiesInfo));
		}
		return grantedAuthorities;
	}

	@Override
	public String getUsername() {
		return userName;
	}

	@Override
	public boolean isAccountNonExpired() {
		return true;
	}

	@Override
	public boolean isAccountNonLocked() {
		return true;
	}

	@Override
	public boolean isCredentialsNonExpired() {
		return true;
	}

	@Override
	public boolean isEnabled() {
		if ("ANON".equals(userType)) {
			return false;
		}
		return true;
	}

	public String getAvatar() {
		return avatar;
	}

	public void setAvatar(String avatar) {
		this.avatar = avatar;
	}

	public String getProvider() {
		return provider;
	}

	public void setProvider(String provider) {
		this.provider = provider;
	}

	public String getValidCode() {
		return validCode;
	}

	public void setValidCode(String validCode) {
		this.validCode = validCode;
	}

}